﻿Public Class Settings
  Private _curIni As AvianWaves.Ini.File = Nothing

  Public Sub New()
    InitializeComponent()
    Me.NumericUpDownNewTime.Maximum = Integer.MaxValue
  End Sub

  Public Sub SetIni(ByRef curIni As AvianWaves.Ini.File)
    Dim tos As New List(Of Integer)

    Me._curIni = curIni
    If Me._curIni.Section("Timeouts") IsNot Nothing Then
      With Me._curIni.Section("Timeouts")
        For Each k As String In .Keys
          Dim x As Integer = .IntegerValueNoNull(k)
          If x > 0 Then
            tos.Add(x)
          End If
        Next
      End With
    End If

    If tos.Count <= 0 Then
      tos.Add(15)
      tos.Add(30)
      tos.Add(60)
      tos.Add(120)
      tos.Add(180)
    End If

    tos.Sort()

    For Each i As Integer In tos
      Me.ListBoxTimeouts.Items.Add(i.ToString)
    Next
  End Sub

  Private Sub MenuItemCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemCancel.Click
    Me.DialogResult = Windows.Forms.DialogResult.Cancel
    Me.Close()
  End Sub

  Private Sub MenuItemOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemOK.Click
    Dim last As Integer = 0

    If Me._curIni.Section("Timeouts") IsNot Nothing Then
      Me._curIni.RemoveSection("Timeouts")
    End If

    Me._curIni.AddSection("Timeouts")

    For i As Integer = 0 To Me.ListBoxTimeouts.Items.Count - 1
      Try
        Dim x As Integer = Integer.Parse(Me.ListBoxTimeouts.Items(i).ToString)
        Me._curIni.Section("Timeouts").IntegerValue("T" & last.ToString) = x
        last += 1
      Catch ex As Exception
      End Try
    Next

    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
  End Sub

  Private Sub NumericUpDownNewTime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownNewTime.ValueChanged
    If NumericUpDownNewTime.Value > 0 Then
      Me.WP7ButtonAdd.Enabled = True
    Else
      Me.WP7ButtonAdd.Enabled = False
    End If
  End Sub

  Private Sub ListBoxTimeouts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBoxTimeouts.SelectedIndexChanged
    If Me.ListBoxTimeouts.Items.Count > 1 AndAlso Me.ListBoxTimeouts.SelectedIndex >= 0 Then
      Me.WP7ButtonRemove.Enabled = True
    Else
      Me.WP7ButtonRemove.Enabled = False
    End If
  End Sub

  Private Sub WP7ButtonAdd_Click(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonAdd.MouseDown
    If NumericUpDownNewTime.Value > 0 Then
      Me.AddValueToListbox(NumericUpDownNewTime.Value)
      NumericUpDownNewTime.Value = 0
    End If
  End Sub

  Private Sub WP7ButtonRemove_Click(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles WP7ButtonRemove.MouseDown
    If Me.ListBoxTimeouts.Items.Count > 1 AndAlso Me.ListBoxTimeouts.SelectedIndex >= 0 Then
      Me.ListBoxTimeouts.Items.RemoveAt(Me.ListBoxTimeouts.SelectedIndex)
    End If
  End Sub

  Private Sub AddValueToListbox(ByVal value As Integer)
    Dim remove As New List(Of Integer)
    Dim added As Boolean = False

    For i As Integer = 0 To Me.ListBoxTimeouts.Items.Count - 1
      Try
        Dim x As Integer = Integer.Parse(Me.ListBoxTimeouts.Items(i).ToString)
        If value = x Then
          added = True
          Exit For
        ElseIf value < x Then
          Me.ListBoxTimeouts.Items.Insert(i, value)
          added = True
          Exit For
        End If
      Catch ex As Exception
        remove.Add(i)
      End Try
    Next

    For Each i As Integer In remove
      Me.ListBoxTimeouts.Items.RemoveAt(i)
    Next

    If Not added Then
      Me.ListBoxTimeouts.Items.Add(value)
    End If
  End Sub
End Class